package com.crm.service.impl.sys.permissionManagement;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.permissionManagement.SysCompanyPermissionCustomMapper;
import com.crm.dao.mapper.sys.permissionManagement.SysCompanyPermissionMapper;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompanyPermission;
import com.crm.model.entity.sys.permissionManagement.SysCompanyPermissionExample;
import com.crm.service.sys.permissionManagement.SysCompanyPermissionDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yincuimei
 * @Date 2020/12/6 19:25
 */
@Service
public class SysCompanyPermissionDbServiceImpl implements SysCompanyPermissionDbService {
    @Autowired
    SysCompanyPermissionCustomMapper sysCompanyPermissionCustomMapper;

    @Autowired
    SysCompanyPermissionMapper sysCompanyPermissionMapper;
    /**
     * 批量添加公司-(菜单-权限组关联)关联表数据
     * @param sysCompanyPermissionList
     * @return
     */
    @Override
    public Boolean insertSysCompanyPermissionBatch(List<SysCompanyPermission> sysCompanyPermissionList) {
        return sysCompanyPermissionCustomMapper.insertSysCompanyPermissionBatch(sysCompanyPermissionList) > 0;
    }

    @Override
    public int batchDelSysCompanyPermissionByMPIds(List<String> menuPermissionIds) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        return sysCompanyPermissionCustomMapper.batchDelSysCompanyPermissionByMPIds(menuPermissionIds, userAccount.getAccountId(), new Date());
    }

    @Override
    public List<SysCompanyPermission> selectSysCompanyPermissionList(List<String> menuPermissionIdList) {
        SysCompanyPermissionExample sysCompanyPermissionExample = new SysCompanyPermissionExample();
        SysCompanyPermissionExample.Criteria criteria = sysCompanyPermissionExample.createCriteria();
        if (menuPermissionIdList != null && menuPermissionIdList.size() > 0) {
            criteria.andMenuPermissionIdIn(menuPermissionIdList);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        return sysCompanyPermissionMapper.selectByExample(sysCompanyPermissionExample);
    }
}
